package com.nbcio.iot.controller;

import com.nbcio.common.core.domain.R;
import com.nbcio.common.core.validate.AddGroup;
import com.nbcio.common.core.validate.EditGroup;
import com.nbcio.common.mybatis.core.page.TableDataInfo;
import com.nbcio.iot.domain.bo.IotDeviceBo;
import com.nbcio.iot.domain.vo.IotDeviceVo;
import com.nbcio.iot.service.IIotDeviceService;
import lombok.RequiredArgsConstructor;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 设备Controller
 */
@Validated
@RequiredArgsConstructor
@RestController
@RequestMapping("/iot/device")
public class IotDeviceController {

    private final IIotDeviceService deviceService;

    /**
     * 查询设备列表
     */
    @GetMapping("/list")
    public R<List<IotDeviceVo>> list(IotDeviceBo bo) {
        List<IotDeviceVo> list = deviceService.queryList(bo);
        return R.ok(list);
    }

    /**
     * 分页查询设备列表
     */
    @GetMapping("/page")
    public TableDataInfo<IotDeviceVo> page(IotDeviceBo bo) {
        List<IotDeviceVo> list = deviceService.queryList(bo);
        return TableDataInfo.build(list);
    }

    /**
     * 根据分组ID查询设备列表
     */
    @GetMapping("/group/{groupId}")
    public R<List<IotDeviceVo>> listByGroupId(@PathVariable Long groupId) {
        List<IotDeviceVo> list = deviceService.queryListByGroupId(groupId);
        return R.ok(list);
    }

    /**
     * 获取设备详细信息
     */
    @GetMapping("/{deviceId}")
    public R<IotDeviceVo> getInfo(@PathVariable Long deviceId) {
        return R.ok(deviceService.queryById(deviceId));
    }

    /**
     * 新增设备
     */
    @PostMapping
    public R<Boolean> add(@Validated(AddGroup.class) @RequestBody IotDeviceBo bo) {
        return R.ok(deviceService.insertByBo(bo));
    }

    /**
     * 修改设备
     */
    @PutMapping
    public R<Boolean> edit(@Validated(EditGroup.class) @RequestBody IotDeviceBo bo) {
        return R.ok(deviceService.updateByBo(bo));
    }

    /**
     * 删除设备
     */
    @DeleteMapping("/{deviceIds}")
    public R<Boolean> remove(@PathVariable Long[] deviceIds) {
        return R.ok(deviceService.deleteWithValidByIds(List.of(deviceIds), true));
    }

    /**
     * 按设备编码获取设备基础信息（用于规则快速解析deviceId）
     */
    @GetMapping("/getByCode")
    public R<Map<String, Object>> getByCode(@RequestParam("deviceCode") String deviceCode) {
        IotDeviceBo bo = new IotDeviceBo();
        bo.setDeviceCode(deviceCode);
        List<IotDeviceVo> list = deviceService.queryList(bo);
        if (list == null || list.isEmpty()) {
            return R.fail("设备不存在: " + deviceCode);
        }
        IotDeviceVo vo = list.get(0);
        Map<String, Object> resp = new HashMap<>();
        resp.put("deviceId", vo.getDeviceId());
        resp.put("deviceCode", vo.getDeviceCode());
        resp.put("productId", vo.getProductId());
        resp.put("flowId", vo.getFlowId());
        return R.ok(resp);
    }

    /**
     * 获取设备采集配置（解析protocol_config_override），用于规则按设备自动读取参数
     */
    @GetMapping("/getCollectConfig")
    public R<Map<String, Object>> getCollectConfig(@RequestParam("deviceCode") String deviceCode) {
        IotDeviceBo bo = new IotDeviceBo();
        bo.setDeviceCode(deviceCode);
        List<IotDeviceVo> list = deviceService.queryList(bo);
        if (list == null || list.isEmpty()) {
            return R.fail("设备不存在: " + deviceCode);
        }
        IotDeviceVo vo = list.get(0);
        // 这里直接返回设备视图对象中的协议覆盖字段，前端/规则可按需解析
        Map<String, Object> resp = new HashMap<>();
        resp.put("protocolConfigOverride", vo.getProtocolConfigOverride());
        return R.ok(resp);
    }
} 